﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VPLink2013.Framework;

namespace VPLink2014.Data.Notifications
{
    public static class NotificationsEntitiesExtension
    {
        public static Notification GetUserNotifications(this VPLinkLogEntities input, string login)
        {
            return input.Notifications.Where(x => x.LoginUser.Equals(login, StringComparison.CurrentCultureIgnoreCase))
                 .FirstOrDefault();
        }

        public static void SetUserNotification(this VPLinkLogEntities input, Notification notifs, bool addToNotifs)
        {
            if (addToNotifs)
                input.Notifications.Add(notifs);

            input.SaveChanges();
        }

        private static void CreateNewsListAddNewItem(string sourceName, string sourceId, NewsItem newsItem, WorkSpace userWorkSpace)
        {
            NewsList newsSource = new NewsList(sourceId, sourceName);
            newsSource.listItems.Add(newsItem);
            userWorkSpace.NewsSources.Add(newsSource);
        }

        private static NotificationsItem CreateNotificationsObj(string spaceName, string sourceName, string sourceId, string itemName, string itemId, NotificationsItem notifications)
        {
            if (notifications != null)
            {
                WorkSpace userWorkSpace = notifications.WorkSpaces.Where(workSpace => workSpace.Name.Equals(spaceName)).FirstOrDefault();
                if (userWorkSpace != null)
                {
                    NewsList newsSource = userWorkSpace.NewsSources.Where(source => source.Id.Equals(sourceId)).FirstOrDefault();
                    if (newsSource != null)
                    {
                        NewsItem newsItem = newsSource.listItems
                            .Where(item => item.Id.Equals(itemId) || item.Name.Equals(itemName))
                            .FirstOrDefault();
                        if (newsItem != null)
                        {
                            newsItem.LastModified = DateTime.Now.ToString();
                            newsItem.Name = itemName;
                        }
                        else
                        {
                            newsItem = new NewsItem(itemId, itemName);
                            newsSource.listItems.Add(newsItem);
                        }
                    }
                    else
                    {
                        NewsItem newsItem = new NewsItem(itemId, itemName);
                        CreateNewsListAddNewItem(sourceName, sourceId, newsItem, userWorkSpace);
                    }
                }
                else
                {
                    NewsItem newsItem = new NewsItem(itemId, itemName);
                    userWorkSpace = new WorkSpace(spaceName);
                    CreateNewsListAddNewItem(sourceName, sourceId, newsItem, userWorkSpace);
                    notifications.WorkSpaces.Add(userWorkSpace);
                }
            }
            else
            {
                NewsItem newsItem = new NewsItem(itemId, itemName);
                List<WorkSpace> spaces = new List<WorkSpace>();
                WorkSpace userWorkSpace = new WorkSpace(spaceName);
                CreateNewsListAddNewItem(sourceName, sourceId, newsItem, userWorkSpace);
                spaces.Add(userWorkSpace);
                notifications = new NotificationsItem() { WorkSpaces = spaces };
            }
            return notifications;
        }

        public static void SaveUpdatedItemToNotifications(this VPLinkLogEntities input, string loginUser,
            string spaceName,
            string sourceName,
            string sourceId,
            string itemName,
            string itemId)
        {
            try
            {
                var notifs = input.GetUserNotifications(loginUser);
                NotificationsItem notifications = null;
                bool newNotif = false;
                if (notifs != null)
                {
                    notifications = FileSerialization.CreateObjFromXMLString<NotificationsItem>(notifs.XmlNotifications);
                }
                else
                {
                    // Create new Obj in db
                    newNotif = true;
                    notifs = new Notification();
                    notifs.LoginUser = loginUser;
                }
                notifications = CreateNotificationsObj(spaceName, sourceName, sourceId, itemName, itemId, notifications);
                notifs.XmlNotifications = FileSerialization.CreateXmlStringFromObj(notifications);
                input.SetUserNotification(notifs, newNotif);
            }
            catch (Exception ex)
            {
                ContextTrace.AddTrace(ex.Message);
                ContextTrace.AddTrace(ex.StackTrace);
            }
        }
    }
}
